import json
from config import Config
import os


def handle():
    """
    将获取的txt整理处理成知识库
    :return:
    """
    question_vec = []
    all_data = []
    number = []
    count = 0
    for line in open(os.path.join(Config.DATA_HOME, 'exam.txt'), encoding="gbk"):
        line = line.strip("\n")
        count = count + 1
        all_data.append(line)
        if line.find("？") != -1:
            number.append(count)
    for i in range(1, len(number)):
        text = all_data[number[i - 1]:number[i] - 1]
        if text[-1].find("章") != -1:
            text = text[:-2]
        text = [element for element in text if not element.isdigit()]
        question = "".join(all_data[number[i - 1] - 1].split(".")[1:])
        question_vec.append({"question": question, "answer": text})
    with open(os.path.join(Config.DATA_HOME, 'kg.json'), "w", encoding="utf8")as f:
        json.dump(question_vec, f, ensure_ascii=False, indent=2)
if __name__ == '__main__':
    handle()